include_directories(.)

include_directories($ENV{INFORMIX_CSDK_INCL}/c++)
include_directories($ENV{INFORMIX_CSDK_INCL}/cpp)
include_directories($ENV{INFORMIX_CSDK_INCL}/dmi)
include_directories($ENV{INFORMIX_CSDK_INCL}/esql)

LINK_DIRECTORIES(${PROJECT_SOURCE_DIR}/lib)

LINK_DIRECTORIES($ENV{INFORMIX_CSDK_LIB})
LINK_DIRECTORIES($ENV{INFORMIX_CSDK_LIB}/c++)
LINK_DIRECTORIES($ENV{INFORMIX_CSDK_LIB}/cpp)
LINK_DIRECTORIES($ENV{INFORMIX_CSDK_LIB}/dmi)
LINK_DIRECTORIES($ENV{INFORMIX_CSDK_LIB}/esql) 

set (SRCS
    ${PROJECT_SOURCE_DIR}/src/topdbs_common.cpp
    ${PROJECT_SOURCE_DIR}/src/topdbs_base_session.cpp
    ${PROJECT_SOURCE_DIR}/src/topdbs_string.cpp
    ${PROJECT_SOURCE_DIR}/src/topdbs_field.cpp
    ${PROJECT_SOURCE_DIR}/src/topdbs_logutil.cpp
    informix_connection.cpp 
    informix_session.cpp
    )
    
if("${CMAKE_CXX_COMPILER_ID}" MATCHES "MSVC")
SET(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS TRUE)   
endif()  

##set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR}/lib)

##ar -crs libifx_checkapi.a ${INFORMIX_CSDK_LIB}/esql/checkapi.o
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
    add_custom_command(OUTPUT ifx_checkapi
        COMMAND ar crsT libifx_checkapi.a $ENV{INFORMIXDIR}/lib/esql/checkapi.o 
        COMMAND cp libifx_checkapi.a ${PROJECT_SOURCE_DIR}/lib/
        COMMAND cp libifx_checkapi.a ${LIBRARY_OUTPUT_PATH}/
        )
    # Merge library    
    add_custom_target(libifx_checkapi.a ALL DEPENDS ifx_checkapi)
    install(FILES ${LIBRARY_OUTPUT_PATH}/libifx_checkapi.a DESTINATION ${LIBRARY_INSTALL_PATH})
endif()

##esql -e -o informix_esql.c informix_esql.ec
add_custom_command(OUTPUT informix_esql.c
    COMMAND esql -e -o informix_esql.c ${PROJECT_SOURCE_DIR}/src/database/informix/informix_esql.ec 
    )   
#add_custom_target(_merge ALL DEPENDS libifx_checkapi.a informix_esql.c) 

add_library(topdbs_ifxesql SHARED informix_esql.c )
if("${CMAKE_CXX_COMPILER_ID}" MATCHES "MSVC")
    target_link_libraries(topdbs_ifxesql isqlt09a libthdmi)
else()
    target_link_libraries(topdbs_ifxesql ifdmi ifasf ifsql ifgen ifgls ifos ifglx crypt ifx_checkapi) 
endif()
install(TARGETS topdbs_ifxesql DESTINATION ${LIBRARY_INSTALL_PATH})

add_library(topdbs_informix SHARED ${SRCS})
if("${CMAKE_CXX_COMPILER_ID}" MATCHES "MSVC")
    target_link_libraries(topdbs_informix isqlt09a libthdmi libthc++)
else()
    add_dependencies(topdbs_informix libifx_checkapi.a)
    target_link_libraries(topdbs_informix ifc++ ifdmi ifasf ifsql ifgen ifgls ifos ifglx crypt ifx_checkapi) 
endif()

install(TARGETS topdbs_informix DESTINATION ${LIBRARY_INSTALL_PATH})

set (HEADERS
    informix_connection.hpp
    informix_session.hpp
    informix_esql.h
    )

install(FILES ${HEADERS} DESTINATION ${INCLUDE_INSTALL_PATH}/topdbs/informix)
